<!DOCTYPE html>
<html>
<head>
<link rel="help" href="http://www.w3.org/TR/DOM-Level-3-Events/#events-KeyboardEvent-getModifierState" />
<script src="../../resources/js-test.js"></script>
<script src="resources/input-modifiers.js"></script>
</head>
<body>
<script>
description("Tests KeyboardEvent.getModifierState()");

var lastKeyboardEvent;

function recordKeyEvent(ev) {
    ev = ev || event;
    ev.keyCode = (ev.which || ev.keyCode);
    if (window.eventSender) {
        lastKeyboardEvent = ev;
    } else {
        var debugString = "Type=" + ev.type + ",keyCode=" + ev.keyCode + ",";
        forEachModifier(function(eventInitName, eventModifierName, eventSenderName) {
            debugString += eventModifierName + "=" + ev.getModifierState(eventModifierName) + ",";
        });
        debug(debugString);
    }
}

function testKeyEventWithModifiers(evString, eventModifiers, expectedKeyCode) {
    eventSender.keyDown(evString, eventModifiers);
    shouldBe("lastKeyboardEvent.type", '"keydown"');
    shouldEvaluateTo("lastKeyboardEvent.keyCode", expectedKeyCode);

    forEachModifier(function(modifierInitName, modifierName, eventSenderName) {
        var expectedModifierState = eventModifiers.indexOf(eventSenderName) >= 0;
        shouldEvaluateTo("lastKeyboardEvent.getModifierState('" + modifierName + "')", expectedModifierState);
    });

    forEachLegacyModifier(function(attr) {
        var expectedModifierState = eventModifiers.indexOf(attr) >= 0;
        shouldEvaluateTo("lastKeyboardEvent." + attr, expectedModifierState);
    });
}

var textarea = document.createElement("textarea");
textarea.addEventListener("keydown", recordKeyEvent, false);
document.body.insertBefore(textarea, document.body.firstChild);
textarea.focus();

if (window.eventSender) {

    testKeyEventWithModifiers("PageUp", ["ctrlKey"], 33);
    testKeyEventWithModifiers("PageDown", ["shiftKey"], 34);
    testKeyEventWithModifiers("Home", ["altKey"], 36);
    testKeyEventWithModifiers("End", ["metaKey"], 35);
    testKeyEventWithModifiers("ArrowLeft", ["ctrlKey", "shiftKey"], 37);
    testKeyEventWithModifiers("ArrowRight", ["ctrlKey", "shiftKey", "altKey"], 39);
    testKeyEventWithModifiers("ArrowUp", ["ctrlKey", "shiftKey", "altKey", "metaKey"], 38);

    forEachModifier( function(eventInitName, eventModifierName, eventSenderName) {
      testKeyEventWithModifiers("ArrowLeft", [eventSenderName], 37);
    });

    var isMacOSX = navigator.userAgent.indexOf("Mac OS X") != -1;
    eventSender.keyDown("PageUp", [isMacOSX ? "metaKey" : "ctrlKey"]);
    shouldBe("lastKeyboardEvent.type", '"keydown"');
    shouldEvaluateTo("lastKeyboardEvent.keyCode", 33);
    shouldEvaluateTo("lastKeyboardEvent.getModifierState('Accel')", "true");
} else {
    debug("This test requires DumpRenderTree.  To manually test, 1) focus on the textarea above and press keys while holding modifiers 2) see if the ctrlKey/shiftKey/altKey/metaKey= values are correctly reported.");
}
</script>
</body>
</html>
